<template>
  <div class="law-reg-info">
    <data-table :CardTable="infoList" :split_text_length="30"></data-table>
    <a-modal
      :visible="visible"
      @cancel="ReghandleCancel"
      @ok="ReghandleOk"
      :title="RegTitle"
      centered
      :maskClosable="false"
    >
      <a-form>
        <a-form-item label="法律法规名称" required>
          <a-input v-model="formData.name" placeholder="请输入法律法规名称" />
        </a-form-item>
      </a-form>
    </a-modal>
  </div>
</template>

<script>
import dataTable from "@/components/data-table.vue";
const request_api = "/cases/law/list";
const request_edit_api = "/cases/law/edit";
const request_del_api = "/cases/law/del";
const column = [
  {
    title: "法律法规名称",
    dataIndex: "name",
    scopedSlots: { customRender: "splitText" }
  },
  {
    title: "操作",
    scopedSlots: { customRender: "operate" }
  }
];
export default {
  name: "law-reg-info",
  components: {
    dataTable
  },
  data() {
    return {
      RegTitle: "添加",
      visible: false,
      infoList: {
        handleArr: [],
        columns: [],
        data: [],
        searchInput: [],
        titleArr: [],
        pagination: {}
      },
      formData: {
        name: ""
      }
    };
  },
  props: {
    data: { require: true }
  },
  methods: {
    onInit() {
      let _this = this;
      this.$axios.post(request_api).then(res => {
        let list = res.list;
        list.forEach(function(item) {
          item.key = item.id;
          item.rowKey = item.id;
          item.handle = [
            { name: "编辑", callback: _this.handleTableEdit },
            { name: "删除", callback: _this.handleTableDel }
          ];
        });
        _this.infoList.data = list;
      });
    },
    handleTableEdit(data) {
      this.formData = this.$tools.deepClone(data);
      this.RegTitle = "编辑法律法规";
      this.visible = true;
    },
    handleTableDel(data) {
      let _this = this;
      this.$confirm({
        title: "确定删除吗？",
        onOk() {
          _this.$axios.post(request_del_api, { id: data.id }).then(res => {
            if (res.ok) {
              _this.$message.success("删除成功");
              _this.onInit();
            } else {
              _this.$message.error(res.error);
            }
          });
        },
        onCancel() {}
      });
    },
    ReghandleCancel() {
      this.formData = {
        name: ""
      };
      this.visible = false;
    },
    ReghandleOk() {
      let _this = this;
      let server_data = { ...this.formData };
      if (server_data.name == "") {
        this.$message.error("请填写法律法规名称!");
        return;
      }
      let $msg = "";
      if (server_data.id) {
        $msg = "修改成功！";
      } else {
        $msg = "添加成功！";
      }
      this.$axios.post(request_edit_api, server_data).then(res => {
        if (res.ok) {
          _this.$message.success($msg);
          _this.onInit();
        } else {
          _this.$message.error(res.error);
        }
      });
      this.ReghandleCancel();
    }
  },
  created() {
    this.infoList.columns = [].concat([], column);
    this.onInit();
  }
};
</script>

<style scoped></style>
